package com.liziba.es.util;

import com.liziba.es.pojo.SearchContent;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther LiZiBa
 * @date 2021/2/26 0:24
 * @description: html 解析工具类
 **/
public class HtmlParseUtil {

    /**
     * 爬虫地址
     */
    private static final String JD_PARSE_URL = "https://search.jd.com/Search?keyword=";
    /**
     * 爬虫编码后缀
     */
    private static final String JD_PARSE_CHARSET = "&enc=utf-8";

    /**
     * 爬取京东数据
     * @param searchKey     查询关键字
     * @return
     * @throws IOException
     */
    public static List<SearchContent> parseJDSearchContent(String searchKey) throws IOException {
        List<SearchContent> list = new ArrayList<>();
        String url = String.format("%s%s%s", JD_PARSE_URL, searchKey, JD_PARSE_CHARSET);
        Document document = Jsoup.parse(new URL(new String(url.getBytes(), "UTF-8")), 3000);

        Element j_goodsList = document.getElementById("J_goodsList");
        Elements li = j_goodsList.getElementsByTag("li");
        for (Element element : li) {
            // 图片地址
            String src = element.getElementsByTag("img").eq(0).attr("src");
            // 价格
            String price = element.getElementsByClass("p-price").eq(0).text();
            // 名称
            String name = element.getElementsByClass("p-name").eq(0).text();
            list.add(new SearchContent(src,price,name));
        }
        return list;
    }


    /**
     * 简单测试
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        List<SearchContent> list = parseJDSearchContent("java");
        list.forEach(System.out::println);
    }

}
